package com.gxt.finance.service.impl;

import com.gxt.aftermarket.domain.Customer;
import com.gxt.aftermarket.mapper.CustomerMapper;
import com.gxt.basic.utils.LoginContext;
import com.gxt.finance.domain.Deposit;
import com.gxt.finance.dto.DepositPayDTO;
import com.gxt.finance.mapper.DepositMapper;
import com.gxt.finance.service.IDepositService;
import com.gxt.basic.service.impl.BaseServiceImpl;
import com.gxt.maket.domain.Business;
import com.gxt.maket.domain.Clue;
import com.gxt.maket.mapper.BusinessMapper;
import com.gxt.maket.mapper.ClueMapper;
import com.gxt.order.domain.Order;
import com.gxt.order.domain.OrderProduct;
import com.gxt.order.mapper.OrderActivityMapper;
import com.gxt.order.mapper.OrderMapper;
import com.gxt.order.mapper.OrderProductMapper;
import com.gxt.org.domain.Employee;
import com.gxt.prod.domain.Product;
import com.gxt.prod.mapper.ProductMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gxt
 * @since 2022-10-26
 */
@Service
public class DepositServiceImpl extends BaseServiceImpl<Deposit> implements IDepositService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderProductMapper orderProductMapper;
    @Autowired
    private OrderActivityMapper orderActivityMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private DepositMapper depositMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    @Transactional
    public void pay(DepositPayDTO depositPayDTO) {
        Long businessId = depositPayDTO.getBusinessId();
        List<Long> activityIds = depositPayDTO.getActivityIds();
        Employee loginUser = LoginContext.getLoginUser();
        Business business = businessMapper.loadById(businessId);
        Product product = productMapper.loadById(business.getProductId());
        Clue clue = clueMapper.loadById(business.getClueId());

        //创建客户
        Customer customer = new Customer();
        customer.setName(clue.getFullName());
        customer.setTel(clue.getPhone());
        customer.setEmail(clue.getEmail());
        customer.setState(1);
        customer.setSellerId(clue.getOwner());
        customer.setInputuserId(loginUser.getId());
        customerMapper.insert(customer);
        //创建订单
        Order order = new Order();
        order.setSn(UUID.randomUUID().toString());
        order.setBusinessId(businessId);
        order.setBusinessName(business.getName());
        order.setProductId(business.getProductId());
        order.setProductName(business.getProductName());
        /**
         * 产品的架构+营销活动的力度   目前只用产品价格不加优惠
         */
        order.setPrice(business.getProductPrice());
        order.setCustomerId(customer.getId());
        order.setCustomerName(customer.getName());
        order.setState(1);
        orderMapper.insert(order);

        //订单产品
        OrderProduct orderProduct = new OrderProduct();
        BeanUtils.copyProperties(product, orderProduct);
        orderProduct.setId(null);
        orderProduct.setOrderId(order.getId());
        orderProductMapper.insert(orderProduct);

        //订单活动
        orderActivityMapper.batchInsert(order.getId(),activityIds);

        //创建定金数据
        Deposit deposit = new Deposit();
        deposit.setName("收到"+customer.getName()+"已缴纳定金"+ depositPayDTO.getPrice()+"元");
        deposit.setCustomerId(customer.getId());
        deposit.setCustomerName(customer.getName());
        deposit.setDeposit(depositPayDTO.getPrice());
        deposit.setPayTime(new Date());
        deposit.setProductId(product.getId());
        deposit.setProductName(product.getName());
        deposit.setBusinessId(business.getId());
        deposit.setBusinessName(business.getName());
        deposit.setState(1);
        deposit.setOrderId(order.getId());
        deposit.setOrderSn(order.getSn());
        depositMapper.insert(deposit);

        //修改商机状态
        business.setState(1);
        businessMapper.update(business);
    }
}
